class Locale {
    /**
     * 翻译
     * @param {*} locale 语言
     * @param {*} templateId 模板Id
     * @param {*} contentId 内容Id
     */
    async translate(locale, templateId,contentId) {
        const i18n = await this.loadLocale(locale);
        const templateElement = document.getElementById(templateId);
        const translatedContent = await this.replacePlaceholders(templateElement.innerHTML, i18n);
        const contentElement = document.getElementById(contentId);
        contentElement.innerHTML = translatedContent; // 更新内容
    }
    /**
     * 加载语言包
     * @param {*} locale 语言
     * @returns 
     */
    async loadLocale(locale) {
        const response = await fetch(`/assets/i18n/${locale}.json`);
        const data = await response.json();
        const i18n = await data;
        return i18n;
    }
    replacePlaceholders(template, translations) {
        return template.replace(/{{i18n.(\w+)}}/g, (match, key) => {
            return translations[key] || match; // 如果找不到翻译，则返回原占位符
        });
    }
}
